using System;
using System.Collections.Generic;
using System.Linq;
using ApiProject.Api.Data;
using ApiProject.Api.Entity;
using ApiProject.Api.PramaModel;
using ApiProject.Api.Repository;
using ApiProject.Api.Uitls;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Newtonsoft.Json;

//商品分类
namespace ApiProject.Api.Controllers
{
    // [Authorize]
    [ApiController]
    [Route("[controller]")]
    public class FontCommdityController : ControllerBase
    {
        private IRepository<CommodityDetails> _commskuRepository;
        private IRepository<Commodities> _commdityRepository;
        private IRepository<CommodityDetailPicture> _commpictureRepository;

        public FontCommdityController(
            IRepository<CommodityDetails> commskuRepository,
            IRepository<CommodityDetailPicture> commpictureRepository,
            IRepository<Commodities> commdityRepository
        )
        {
            _commskuRepository = commskuRepository;
            _commdityRepository=commdityRepository;
            _commpictureRepository=commpictureRepository;

        }

    
        // 获取全部spu
        // Get /commodity
        [HttpGet]
        public dynamic GetAllSpu()
        {
            // // var skubyuserId=_commskuRepository.Table.Where(x=>x.UserId==id && x.IsDelete==false).ToList();

            var spu = from commsku in _commskuRepository.Table
                      join commspu in _commdityRepository.Table on commsku.CommodityId equals commspu.Id
                      where commsku.IsDelete == false && commsku.IsGrounding==true
                      select new { Id = commsku.Id, CommodityId=commspu.Id,CommodityName=commspu.CommodityName,
                        CommodityExplain=commspu.CommodityExplain,Price=commsku.Price,Inventory=commsku.Inventory,
                        CreateTime=commsku.CreateTime,IsEnable=commsku.IsGrounding
                      };


            

            return JsonHelper.Serialize(new
            {
                Code = 200,
                Data = spu,
                Msg = "查询成功"
            });
        }

        //获取所有商品图片
        [HttpGet,Route("spupicture")]
        public dynamic GetCommSpupicture(){

            var spupicture=_commpictureRepository.Table.ToList();

            return JsonHelper.Serialize(new
            {
                Code = 200,
                Data = spupicture,
                Msg = "查询成功"
            });
        }

        //通过商品Id获取对应图片
        [HttpGet,Route("spupicturebyId/{id}")]
         public dynamic GetCommSpupicturebyId(int id){

            var spupicture=_commpictureRepository.Table.Where(x=>x.CommodityId==id && x.IsDelete==false).ToList();
            return JsonHelper.Serialize(new
            {
                Code = 200,
                Data = spupicture,
                Msg = "查询成功"
            });
        }

        
    }
}
